package com.ggh.jinjilive.ui.mine;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityMineDealDetailBinding;
import com.ggh.jinjilive.mobile.mine.TradingDetailMobile;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.mine.adapter.MineDealDetailAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

@Route(path = ARouterUtils.MineDealDetailActivity)
public class MineDealDetailActivity extends BaseActivity<ActivityMineDealDetailBinding>implements OnRefreshListener, OnLoadMoreListener{
    //VM
    private MineViewModel mineViewModel;
    //适配器
    private MineDealDetailAdapter mineDealDetailAdapter;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        mineViewModel = new ViewModelProvider(this,viewModelFactory).get(MineViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_mine_deal_detail;
    }

    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("交易明细",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);

        loadService = LoadSir.getDefault().register(binding.refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
    }

    @Override
    public void initData() {
        super.initData();
        mineDealDetailAdapter = new MineDealDetailAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        binding.recyclerView.setAdapter(mineDealDetailAdapter);
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        //获取
        onRefresh(binding.refreshLayout);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
        //交易明细
        mineViewModel.mTradingDetailMobileList.observe(this, new Observer<List<TradingDetailMobile>>() {
            @Override
            public void onChanged(List<TradingDetailMobile> tradingDetailMobiles) {
                binding.refreshLayout.finishRefresh();
                if(null != tradingDetailMobiles){
                    if(mineViewModel.isRefresh){
                        if(tradingDetailMobiles.size() > 0){
                            loadService.showSuccess();
                            mineDealDetailAdapter.setAdd(tradingDetailMobiles);
                        }else {
                            loadService.showCallback(EmptyCallback.class);
                        }
                    }else {
                        if(tradingDetailMobiles.size() > 0){
                            mineDealDetailAdapter.Add(tradingDetailMobiles);
                            binding.refreshLayout.finishLoadMore();
                        }else {
                            binding.refreshLayout.finishLoadMoreWithNoMoreData();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        mineViewModel.isRefresh = false;
        mineViewModel.infoLogList();
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        mineViewModel.isRefresh = true;
        mineViewModel.infoLogList();
    }
}
